﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using Microsoft.EntityFrameworkCore;
using PathologyDAL.DB;
using PathologyDAL.Modle;

namespace PathologyDAL.Service
{
    public class ConditionServices
    {
        private readonly Filter filter;

        public ConditionServices(Filter filter)
        {
            this.filter = filter;
        }
        public ConditionServices()
        {
                
        }
        public async Task<int> AddAsync(string Name)
        {
            using (ApplicationDbcontext context = new ApplicationDbcontext())
            {
                //  查询数据库中是否有

                var bv = context
                    .Conditions.Where(x => x.Filter == filter)
                    .FirstOrDefault(b => b.Name == Name);

                if (bv == null)
                {
                    FilterCondition condition = new FilterCondition
                    {
                        Name = Name,
                        Filter = filter,
                        LastUpdated = DateTime.Now,
                    };
                    context.Conditions.Add(condition);
                }
                return await context.SaveChangesAsync();
            }
        }

        public async Task<int> AddAsync(List<string> Names)
        {
            using (ApplicationDbcontext context = new ApplicationDbcontext())
            {
                List<FilterCondition> bvs = new List<FilterCondition>();
                //查询数据库中是否有
                foreach (string name in Names)
                {
                    var bv = context
                        .Conditions.Where(x => x.Filter == filter)
                        .FirstOrDefault(b => b.Name == name);

                    if (bv == null)
                    {
                        FilterCondition condition = new FilterCondition
                        {
                            Name = name.Trim(),
                            Filter = filter,
                            LastUpdated = DateTime.Now,
                        };
                        context.Conditions.Add(condition);
                    }
                }

              //  context.Conditions.AddRange(bvs);
                return await context.SaveChangesAsync();
            }
        }

        public async Task<int> DeleteAsync(int id)
        {
            using (ApplicationDbcontext context = new ApplicationDbcontext())
            {
                var bv = context.Conditions.FirstOrDefault(b => b.Id == id);

                // var bvWithTumber = context.Possibility.Where(x => x.FilterConditionId == id);

                if (bv != null)
                {
                    //  context.Possibility.RemoveRange(bvWithTumber);
                  
                    var tumourConditionBv = context.TumourConditions.Where(t => t.FilterCondition == bv.Id);
                    context.Conditions.Remove(bv);
                    context.TumourConditions.RemoveRange(tumourConditionBv);
                }

               
                return await context.SaveChangesAsync();
            }
        }

        public async Task<int> UpdateAsync(int id, string Name)
        {
            using (ApplicationDbcontext context = new ApplicationDbcontext())
            {
                //先判断名字是否重复，如果是，不修改

                var bvs = context.Conditions.Where(x => x.Filter == filter);
                if (bvs.Any(b => b.Name == Name))
                {
                    return 0;
                }

                var bv = bvs.FirstOrDefault(b => b.Id == id);
                if (bv != null)
                {
                    bv.Name = Name;
                    bv.LastUpdated = DateTime.Now;
                }
                return await context.SaveChangesAsync();
            }
        }

        public async Task<List<FilterCondition>> GetAllConditionByFilterAsync(string Filter)
        {
            using (ApplicationDbcontext context = new ApplicationDbcontext())
            {
                var list = context.Conditions.Where(x => x.Filter == filter);
                if (string.IsNullOrEmpty(Filter))
                {
                    return await list.OrderByDescending(b => b.LastUpdated).ToListAsync();
                }
                else
                {
                    return await list.Where(x => x.Name.Contains(Filter))
                        .OrderByDescending(b => b.LastUpdated)
                        .ToListAsync();
                }
            }
        }

        public async Task<List<FilterCondition>> CheckExistAsync(List<string> names)
        {
            using (ApplicationDbcontext context = new ApplicationDbcontext())
            {
                return await context
                    .Conditions.Where(x => x.Filter == filter)
                    .Where(b => names.Contains(b.Name))
                    .ToListAsync();
            }
        }

        public async Task<List<FilterCondition>> GetAllAsync()
        {
            using (ApplicationDbcontext context = new ApplicationDbcontext())
            {
                return await context.Conditions.ToListAsync();
            }
        }
    }
}
